package com.yyfax.ph.home.datainit;

import com.yyfax.common.CLog;
import com.yyfax.infrastructure.eventbus.EventAgent;
import com.yyfax.infrastructure.mvp.IView;
import com.yyfax.infrastructure.mvp.Presenter;
import com.yyfax.infrastructure.mvp.PresenterFactory;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class OptionsPresenter extends Presenter<OptionsModel, OptionsPresenter.View> {

	public static final String TAG = "OptionsPresenter";

	public static class Generator implements PresenterFactory<OptionsPresenter> {
		@Override
		public OptionsPresenter create() {
			return new OptionsPresenter();
		}
	}

	public interface View extends IView {
		void onGetOptions(OptionsModel.OptionsBean options);
	}

	private View mView;
	private String mRequestPage;

	@Override
	public void attach(View view) {
		mRequestPage = view.getClass().getName();
		mView = view;
		mModel = new OptionsModel();
		EventAgent.register(this);
		CLog.i(TAG, "OptionsPresenter attach mRequestPage=" + mRequestPage);
	}

	@Override
	public void detach() {
		mView = null;
		mModel = null;
		EventAgent.unregister(this);
	}

	public void getOptions(String types) {
		execute(types, FUNCTIONAL_CODE, true, null, mRequestPage);
	}

	@Subscribe(threadMode = ThreadMode.MAIN)
	public void optionsResult(OptionsModel.OptionResult result) {
		if (mRequestPage.equals(result.requestPage)) {
			if (isResponseSuccess(result)) {
				CLog.i(TAG, "rs=" + result.toString());
				mView.onGetOptions(result.getContent());
			}
		}
	}
}
